package com.bpmn.utils;

import cn.hutool.extra.spring.SpringUtil;
import com.bpmn.common.holder.TenantContextHolder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.history.HistoricActivityInstanceQuery;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.repository.DeploymentQuery;
import org.flowable.engine.repository.ModelQuery;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.flowable.engine.runtime.ProcessInstanceQuery;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 查询工具
 *
 * @author rain
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class QueryUtils {

    private static final ProcessEngine PROCESS_ENGINE = SpringUtil.getBean(ProcessEngine.class);

    public static ModelQuery modelQuery() {
        return PROCESS_ENGINE.getRepositoryService().createModelQuery().modelTenantId(TenantContextHolder.getTenantId());
    }

    public static ProcessDefinitionQuery definitionQuery() {
        return PROCESS_ENGINE.getRepositoryService().createProcessDefinitionQuery()
                .processDefinitionTenantId(TenantContextHolder.getTenantId());
    }

    public static DeploymentQuery deploymentQuery() {
        return PROCESS_ENGINE.getRepositoryService().createDeploymentQuery()
                .deploymentTenantId(TenantContextHolder.getTenantId());
    }

    public static DeploymentQuery deploymentQuery(String deploymentId) {
        return deploymentQuery().deploymentId(deploymentId);
    }

    public static DeploymentQuery deploymentQuery(List<String> deploymentIds) {
        return deploymentQuery().deploymentIds(deploymentIds);
    }

    public static HistoricTaskInstanceQuery hisTaskInstanceQuery() {
        return PROCESS_ENGINE.getHistoryService().createHistoricTaskInstanceQuery()
                .taskTenantId(TenantContextHolder.getTenantId());
    }

    public static HistoricTaskInstanceQuery hisTaskInstanceQuery(String processInstanceId) {
        return hisTaskInstanceQuery().processInstanceId(processInstanceId);
    }

    public static HistoricTaskInstanceQuery hisTaskBusinessKeyQuery(String businessKey) {
        return hisTaskInstanceQuery().processInstanceBusinessKey(businessKey);
    }

    public static ProcessInstanceQuery instanceQuery() {
        return PROCESS_ENGINE.getRuntimeService().createProcessInstanceQuery()
                .processInstanceTenantId(TenantContextHolder.getTenantId());
    }

    public static ProcessInstanceQuery instanceQuery(String processInstanceId) {
        return instanceQuery().processInstanceId(processInstanceId);
    }

    public static ProcessInstanceQuery businessKeyQuery(String businessKey) {
        return instanceQuery().processInstanceBusinessKey(businessKey);
    }

    public static ProcessInstanceQuery instanceQuery(Set<String> processInstanceIds) {
        return instanceQuery().processInstanceIds(processInstanceIds);
    }

    public static HistoricProcessInstanceQuery hisInstanceQuery() {
        return PROCESS_ENGINE.getHistoryService().createHistoricProcessInstanceQuery()
                .processInstanceTenantId(TenantContextHolder.getTenantId());
    }

    public static HistoricProcessInstanceQuery hisInstanceQuery(String processInstanceId) {
        return hisInstanceQuery().processInstanceId(processInstanceId);
    }

    public static HistoricProcessInstanceQuery hisBusinessKeyQuery(String businessKey) {
        return hisInstanceQuery().processInstanceBusinessKey(businessKey);
    }

    public static HistoricProcessInstanceQuery hisInstanceQuery(Set<String> processInstanceIds) {
        return hisInstanceQuery().processInstanceIds(processInstanceIds);
    }

    public static HistoricActivityInstanceQuery hisActivityInstanceQuery() {
        return PROCESS_ENGINE.getHistoryService().createHistoricActivityInstanceQuery()
                .activityTenantId(TenantContextHolder.getTenantId());
    }

    public static HistoricActivityInstanceQuery hisActivityInstanceQuery(String processInstanceId) {
        return hisActivityInstanceQuery().processInstanceId(processInstanceId);
    }

    public static TaskQuery taskQuery() {
        return PROCESS_ENGINE.getTaskService().createTaskQuery().taskTenantId(TenantContextHolder.getTenantId());
    }

    public static TaskQuery taskQuery(String processInstanceId) {
        return taskQuery().processInstanceId(processInstanceId);
    }

    public static TaskQuery taskQuery(Collection<String> processInstanceIds) {
        return taskQuery().processInstanceIdIn(processInstanceIds);
    }

}
